package Service.Student.Imp;

import Dao.DaoFactory;
import Dao.Role.TeacherDao;
import Dao.Student.StudentPaperDao;
import Dao.Student.StudentTitleDao;
import Dao.TitleAndPaper.TitleDao;
import Entity.MyTableData;
import Entity.Role.Teacher;
import Entity.TitleAndPaper.Paper;
import Entity.TitleAndPaper.Title;
import Service.Student.StudentTitleService;
import Util.DBUtil;

import java.sql.Connection;
import java.util.List;

public class StudentTitleServiceImp implements StudentTitleService {
    private Connection conn;
    private StudentTitleDao studentTitleDao;
    private TitleDao titleDao;
    private TeacherDao teacherDao;
    private StudentPaperDao studentPaperDao;

    public StudentTitleServiceImp() {
        studentTitleDao = DaoFactory.getInstance("studentTitleDao");
        titleDao = DaoFactory.getInstance("titleDao");
        teacherDao = DaoFactory.getInstance("teacherDao");
        studentPaperDao = DaoFactory.getInstance("studentPaperDao");
        conn = DBUtil.getConnByDruid();
    }
    @Override
    public MyTableData getTitleInfo(String sno,Integer offset,Integer limit) {
        List<Title> titles = studentTitleDao.getTitleInfo(sno, offset,limit,conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", titles.size(), titles);
    }

    /**
     * 学生选择课题后生成论文（任务书阶段）
     */
    @Override
    public boolean addStudentTitle(Paper paper) {
        boolean flag = studentPaperDao.insertPaper(paper,conn)==1;
        DBUtil.closeConn(conn);
        return flag;
    }

    /**
     * 学生查询课题
     *
     * @param sno
     * @param keyword
     * @return
     */
    @Override
    public MyTableData titleSelect(String sno, String keyword,Integer offset,Integer limit) {
        List<Title> titles = studentTitleDao.titleSelect(sno,keyword,offset,limit,conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", titles.size(), titles);
    }

    /**
     * 根据工号查询教师信息
     *
     * @param tno
     * @return
     */
    @Override
    public Teacher getTeacherInfo(String tno) {
        List<Teacher> teachers = teacherDao.getTeacherByNO(tno,conn);
        DBUtil.closeConn(conn);
        Teacher teacher = new Teacher();
        if (teachers.size()!=0){
            teacher = teachers.get(0);
        }
        return teacher;
    }

}
